#ifndef ROLoader_H
#define ROLoader_H

#include <QObject>
#include <QUrl>
#include <QQmlComponent>

#include "ROExportProperty.h"

#include "ROSyncCreation.h"

class ROLoader:public ROSyncCreation
{
    Q_OBJECT
    QML_NAMED_ELEMENT(ROLoader)

public:
    ROLoader(QObject* parent = nullptr);


    QUrl source() const;
    void setSource(const QUrl &newSource);

    QQmlComponent *sourceComponent() const;
    void setSourceComponent(QQmlComponent *newSourceComponent);

    QObject *current() const;
    void setCurrent(QObject *newCurrent);

signals:
    void sourceChanged();
    void sourceComponentChanged();

    void currentChanged();
    void beginPropertyBindings();

protected:
    void initialize() override;

private:
    void validate();
    QObject* handleCreation(const QStringList& reason) override;
    void clean();

private:
    QUrl m_source;
    QQmlComponent* m_sourceComponent = nullptr;
    bool m_block = true;
    QPointer<QObject> m_current = nullptr;
    QQmlComponent* m_realComponent = nullptr;
    Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged FINAL)
    Q_PROPERTY(QQmlComponent *sourceComponent READ sourceComponent WRITE setSourceComponent NOTIFY sourceComponentChanged FINAL)
    Q_PROPERTY(QObject *current READ current WRITE setCurrent NOTIFY currentChanged FINAL)
};

#endif
